package tms;

import java.util.ArrayList;
import java.util.List;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.util.GenericType;

import tms.model.Account;
import tms.model.Aircraft;
import tms.model.Booking;
import tms.model.Destination;
import tms.model.Flight;


public class RunService {

	/*
	 * Cach chay:
	 * 		- cd vao toi thu muc chua project
	 * 		- mvn clean install
	 * 		- mvn jetty:run
	 * 		- run file nay va xem key qua
	 */
	
	/*
	 * Ve phia service (project nay)
	 *		- Viet them cac ham cac cac entity khac trong file TravelService
	 *		- Cach viet tuong tu nhu cac ham co san trong file TravelService
	 */
	
	/* Ve phia client
	 * File RunService gia lap 1 client goi toi service TravelService
	 * Modify project cu (tro thanh client) nhu sau:
	 * 		- cac ham save-load trong cac entity ko dung nua 
	 * 		- database cung ko su dung nua
	 * 		- tao 1 file moi(ten file de nghi la ServiceHandler.java trong 1 package moi ten la tms.service) 
	 * 		- viet cac ham save-load toi service TravelService (cach viet nhu cac ham static ben duoi)
	 * 		- cac controller khi can save-load thi goi toi file ServiceHandler vua tao
	 * 		- Xin het.
	 */
	
	private static String URL = "http://localhost:8080/rest/TravelService/";
	private static String DESTINATION_URL = URL + Destination.TABLE_NAME.toLowerCase();
	private static String ACCOUNT_URL = URL + Account.TABLE_NAME.toLowerCase();
	private static String BOOKING_URL = URL + Booking.tableName.toLowerCase(); 
	private static String FLIGHT_URL = URL + Flight.tableName.toLowerCase(); 
	private static String AIRCRAFT_URL = URL + Aircraft.TABLE_NAME.toLowerCase(); 
	
	public static void main(String[] args) {

			// worked
			Destination d = getFirstDestination("destName like '%paris%'");
			System.out.println(d.getDestName() + "|"+ d.getDestCode());
			System.out.println("=========================================");
			
			// worked
			List<Destination> ls = getListDestination("");
			for (Destination obj : ls) {
				System.out.println(obj.getDestName());
			}
			System.out.println("=========================================");
			
			Account a = getFirstAccount("email like '%admin%'");
			System.out.println(a.getFullName() + "|" + a.getEmail());
			
			// worked
						List<Account> lsA = getListAccount("");
						for (Account obj : lsA) {
							System.out.println(obj.getEmail());
						}
			
						System.out.println("=========================================");
						
						// worked
						Booking b = getFirstBooking("ticketnumber like '%TKN%'");
						System.out.println(b.getTicketNumber());
						System.out.println("=========================================");
						
						// worked
						List<Booking> lsB = getListBooking("");
						for (Booking obj : lsB) {
							System.out.println(obj.getTicketNumber());
						}
						
						System.out.println("=========================================");
						
						// worked
						Flight f = getFirstFlight("flightname like '%F002%'");
						System.out.println(f.getFlightName());
						System.out.println("=========================================");
						
						// worked
						List<Flight> lsF = getListFlight("");
						for (Flight obj : lsF) {
							System.out.println(obj.getFlightName());
						}
						
						
						System.out.println("=========================================");
						
						// worked
						Aircraft ar = getFirstAircraft("name like '%360%'");
						System.out.println(ar.getName());
						System.out.println("=========================================");
						
						// worked
						List<Aircraft> lsAr = getListAircraft("");
						for (Aircraft obj : lsAr) {
							System.out.println(obj.getName());
						}
						
						
	}

	public static boolean deleteDestination(Destination obj){
		ClientRequest request = new ClientRequest(DESTINATION_URL + "/delete");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean saveDestination(Destination obj){
		ClientRequest request = new ClientRequest(DESTINATION_URL + "/save");
		
		//IMPORTANT: put the object into request
		request.body("application/xml", obj);
		
		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static Destination getFirstDestination(String where){
		ClientRequest request = new ClientRequest(DESTINATION_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Destination> response = request.post(Destination.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<Destination> getListDestination(String where){
		ClientRequest request = new ClientRequest(DESTINATION_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Destination>> response = request.post(new GenericType<List<Destination>>(){});
			return response.getEntity(new GenericType<List<Destination>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Destination>();
		}
	}

	
	
	public static Account getFirstAccount(String where){
		ClientRequest request = new ClientRequest(ACCOUNT_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Account> response = request.post(Account.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static boolean deleteAccount(Account obj){
		ClientRequest request = new ClientRequest(ACCOUNT_URL + "/delete");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean saveAccount(Account obj){
		ClientRequest request = new ClientRequest(ACCOUNT_URL + "/save");
		
		//IMPORTANT: put the object into request
		request.body("application/xml", obj);
		
		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
	public static List<Account> getListAccount(String where){
		ClientRequest request = new ClientRequest(ACCOUNT_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Account>> response = request.post(new GenericType<List<Account>>(){});
			return response.getEntity(new GenericType<List<Account>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Account>();
		}
	}
	
	
	
	public static Booking getFirstBooking(String where){
		ClientRequest request = new ClientRequest(BOOKING_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Booking> response = request.post(Booking.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static boolean deleteBooking(Booking obj){
		ClientRequest request = new ClientRequest(BOOKING_URL + "/delete");

		//IMPORTANT: put the object into request
		request.body("application/xml", obj);

		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean saveBooking(Booking obj){
		ClientRequest request = new ClientRequest(BOOKING_URL + "/save");
		
		//IMPORTANT: put the object into request
		request.body("application/xml", obj);
		
		try {
			ClientResponse<Boolean> response = request.post(Boolean.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
	public static List<Booking> getListBooking(String where){
		ClientRequest request = new ClientRequest(BOOKING_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Booking>> response = request.post(new GenericType<List<Booking>>(){});
			return response.getEntity(new GenericType<List<Booking>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Booking>();
		}
	}
	
	
	public static Flight getFirstFlight(String where){
		ClientRequest request = new ClientRequest(FLIGHT_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Flight> response = request.post(Flight.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<Flight> getListFlight(String where){
		ClientRequest request = new ClientRequest(FLIGHT_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Flight>> response = request.post(new GenericType<List<Flight>>(){});
			return response.getEntity(new GenericType<List<Flight>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Flight>();
		}
	}
	
	public static Aircraft getFirstAircraft(String where){
		ClientRequest request = new ClientRequest(AIRCRAFT_URL + "/first");
		request.body("application/xml", where);
		try {
			ClientResponse<Aircraft> response = request.post(Aircraft.class);
			return response.getEntity();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<Aircraft> getListAircraft(String where){
		ClientRequest request = new ClientRequest(AIRCRAFT_URL + "/list");
		request.body("application/xml", where);
		try {
			ClientResponse<List<Aircraft>> response = request.post(new GenericType<List<Aircraft>>(){});
			return response.getEntity(new GenericType<List<Aircraft>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Aircraft>();
		}
	}
}
